package com.utilviet.baovn;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout.LayoutParams;

import com.utilviet.baovn.Utility.Utility;
import com.utilviet.baovn.define.AnimateViewType;

import java.util.HashMap;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends Activity {

    private View mMenuTriggerView = null;
    private View mContentGroupView = null;
    private View mAlphaBackgroundMenuView = null;
    private boolean mIsAnimatingMenu = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        mContentGroupView = findViewById(R.id.contentGroupView);
        mAlphaBackgroundMenuView = findViewById(R.id.alphaBackgroundMenuView);

        mMenuTriggerView = findViewById(R.id.menuTriggerView);
        mMenuTriggerView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                //show menu

                if (mIsAnimatingMenu == false) {
                    LayoutParams params = (LayoutParams) mMenuTriggerView.getLayoutParams();

                    //collapse
                    if (params.width >= mContentGroupView.getMeasuredWidth()) {
                        hideMenu();
                    } else {
                        showMenu();
                    }
                }

                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mIsAnimatingMenu = true;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    mIsAnimatingMenu = false;
                }

                return true;//fix swipe left to hide menu but also swipe webview
            }
        });

        final WebviewFragment webviewFragment = (WebviewFragment)getFragmentManager().findFragmentById(R.id.webViewFragment);
        webviewFragment.mIWebViewFragment = new IWebVIewFragment() {
            @Override
            public void onSwipeRightShowMenu(WebviewFragment fragment) {
                showMenu();
            }
        };

        MenuFragment menuFragment = (MenuFragment)getFragmentManager().findFragmentById(R.id.menuFragment);
        menuFragment.mIMenuFragment = new IMenuFragment() {
            @Override
            public void menuDidPressedWebSite(MenuFragment menu, HashMap<String, String> website) {
                Log.d("","" + website);
                String url = website.get("url");
                webviewFragment.cleanAndOpenUrl(url);
                hideMenu();
            }
        };

    }

    private void showMenu() {
        LayoutParams params = (LayoutParams)mMenuTriggerView.getLayoutParams();
        params.width = mContentGroupView.getMeasuredWidth();
        Utility.doAnimation(this,mMenuTriggerView, AnimateViewType.LeftIn,500);
        mMenuTriggerView.setLayoutParams(params);

        mAlphaBackgroundMenuView.setAlpha(0.8f);
        Utility.doAnimation(this, mAlphaBackgroundMenuView, AnimateViewType.FadeIn60, 500);
    }

    private void hideMenu() {
        // Dismiss keyboard
        InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (imm.isAcceptingText()) {
            imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }

        //
        Utility.doAnimation(MainActivity.this,mMenuTriggerView, AnimateViewType.LeftOut,500);

        mAlphaBackgroundMenuView.setAlpha(0);
        Utility.doAnimation(this, mAlphaBackgroundMenuView, AnimateViewType.FadeOut, 500);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                LayoutParams params = (LayoutParams)mMenuTriggerView.getLayoutParams();
                params.width = Utility.getDP(MainActivity.this,30);
                mMenuTriggerView.setLayoutParams(params);
            }
        },450);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
